package tazmania.entity;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;

import tazadum.engine.Entity;
import tazadum.engine.Interval;

public class FadeToWhiteEntity implements Entity {
	@Override
	public void precalc(final GLAutoDrawable drawable, final float screenRatio) {
	}

	@Override
	public void update(final GLAutoDrawable drawable, final float globalTime, final float localTime, final float dt, final Interval interval) {
		final GL2 gl = drawable.getGL().getGL2();

		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glOrtho(0, 640, 480, 0, 0.1f, 1000);
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -4.0f);

		gl.glDisable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_BLEND);
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);

		final float a = Math.min(1, localTime * 0.25f);

		gl.glColor4f(1, 1, 1, a);
		gl.glBegin(GL2.GL_QUADS);
		{
			gl.glVertex2f(-10, -10);
			gl.glVertex2f(650, -10);
			gl.glVertex2f(650, 490);
			gl.glVertex2f(-10, 490);
		}
		gl.glEnd();

		gl.glDisable(GL.GL_BLEND);
		// gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glPopMatrix();
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glPopMatrix();
	}

	@Override
	public void destroy(final GLAutoDrawable drawable) {
	}
}
